package com.gaogzhen.algorithm.classic;

import java.util.HashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.TreeMap;

public class ReversePolishNotation {
    public static void main(String[] args) {
        String[] express = {"3", "17", "15", "-", "*", "18", "6", "/", "+"};
        System.out.println(reversePolishNotation(express));
    }

    public static int reversePolishNotation(String[] express) {
        LinkedList<Integer> ret = new LinkedList<>();
        for (String tmp: express) {
            if (tmp.equals("+") || tmp.equals("-") || tmp.equals("*")|| tmp.equals("/")) {
                Integer num2 = ret.pop();
                Integer num1 = ret.pop();
                if (tmp.equals("+"))
                    ret.push(num1 + num2);
                else if (tmp.equals("-"))
                    ret.push(num1 - num2);
                else if (tmp.equals("*"))
                    ret.push(num1 * num2);
               else
                    ret.push(num1 / num2);
            } else {
                ret.push(Integer.parseInt(tmp));
            }
        }
        return ret.pop();
    }
}
